package com.hsj.goldstore.utils.retrofit;

import com.hsj.goldstore.utils.LogUtil;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018/3/22.
 */

public class AppClient {

    public static Retrofit mRetrofit;

    public static Retrofit retrofit() {
        if (null == mRetrofit) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain) throws IOException {
                            Request request = chain.request();
                            LogUtil.d("intercept()", "request====" + request.toString());
                            okhttp3.Response proceed = chain.proceed(request);
                            LogUtil.i("intercept()", "proceed====" + proceed.headers().toString());
                            return proceed;
                        }
                    })
                    .build();

            mRetrofit = new Retrofit.Builder()
                    .baseUrl(ApiStore.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }
        return mRetrofit;
    }

}
